package com.vhall.component.service.preparation.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vhall.component.entity.preparation.dto.*;
import com.vhall.component.entity.preparation.vo.*;
import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.utils.UploadUtil;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.service.preparation.AdmWatermarkService;
import com.vhall.component.service.room.RoomInnerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * AdmWatermarkServiceImpl-
 *
 * @author xing.wang
 * @date 2022/10/27
 */
@Service
@RequiredArgsConstructor
public class AdmWatermarkServiceImpl implements AdmWatermarkService {
    private final PaasService paasService;
    private final RoomInnerService roomInnerService;
    private static final String COLUMN_WATERMARK_ID = "watermark_id";
    private static final String COLUMN_WATERMARK_IMG = "watermark_img";
    private static final String COLUMN_WATERMARK_NAME = "watermark_name";
    private static final String COLUMN_WATERMARK_POSITIONTYPE = "watermark_positiontype";
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<AdmWatermarkListDTO> getWatermarkList(WatermarkListVO vo) {
        Map<String, Object> param = new HashMap<>(8);
        vo = Optional.ofNullable(vo).orElse(new WatermarkListVO());
        if (Objects.isNull(vo.getPageSize()) || vo.getPageSize() <= 1) {
            vo.setPageSize(10);
        }
        if (Objects.isNull(vo.getPage()) || vo.getPage() <= 0) {
            vo.setPage(1);
        }
        Integer pos = (vo.getPage() - 1) * vo.getPageSize();
        param.put("pos", pos);
        param.put("limit", vo.getPageSize());
        JSONArray watermarkList = paasService.getWatermarkList(param);
        watermarkList = Optional.ofNullable(watermarkList).orElse(new JSONArray());
        int size = watermarkList.size();
        List<AdmWatermarkListDTO> result = new ArrayList<>();
        AdmWatermarkListDTO dto;
        for (int i = 0; i < size; i++) {
            JSONObject watermark = watermarkList.getJSONObject(i);
            dto = new AdmWatermarkListDTO();
            String watermarkId = watermark.getString(COLUMN_WATERMARK_ID);
            dto.setWatermarkId(watermarkId);
            dto.setWatermarkImg(watermark.getString(COLUMN_WATERMARK_IMG));
            dto.setWatermarkName(watermark.getString(COLUMN_WATERMARK_NAME));
            dto.setWatermarkPositiontype(watermark.getString(COLUMN_WATERMARK_POSITIONTYPE));
            dto.setCreatedAt(watermark.getString("created_at"));
            dto.setUpdatedAt(watermark.getString("updated_at"));
            dto.setIsDefault(watermarkId.equals(roomInnerService.getDefaultWatermarkTemplateId()));
            result.add(dto);
        }
        result = result.stream().sorted(Comparator.comparing(AdmWatermarkListDTO::getWatermarkId).reversed()).collect(Collectors.toList());
        return result;
    }

    @Override
    public AdmWatermarkCreateDTO createWatermark(AdmWatermarkCreateVO vo) {
        Map<String, Object> param = new HashMap<>(8);
        param.put(COLUMN_WATERMARK_NAME, vo.getWatermarkName());
        param.put(COLUMN_WATERMARK_POSITIONTYPE, vo.getWatermarkPositiontype());
        param.put("watermark_image", UploadUtil.multipartFile2File(vo.getWatermarkImage()));
        JSONObject watermark = Optional.ofNullable(paasService.createWatermarkTemp(param)).orElse(new JSONObject());
        AdmWatermarkCreateDTO dto = new AdmWatermarkCreateDTO();
        dto.setWatermarkId(watermark.getString(COLUMN_WATERMARK_ID));
        return dto;
    }

    @Override
    public AdmWatermarkUpdateDTO updateWatermark(AdmWatermarkUpdateVO vo) {
        Map<String, Object> param = new HashMap<>(8);
        param.put(COLUMN_WATERMARK_ID, vo.getWatermarkId());
        param.put(COLUMN_WATERMARK_NAME, vo.getWatermarkName());
        param.put(COLUMN_WATERMARK_POSITIONTYPE, vo.getWatermarkPositiontype());
        if (Objects.nonNull(vo.getWatermarkImage())) {
            param.put("watermark_image", UploadUtil.multipartFile2File(vo.getWatermarkImage()));
        }
        JSONObject watermark = Optional.ofNullable(paasService.updateWatermarkTemp(param)).orElse(new JSONObject());

        AdmWatermarkUpdateDTO dto = new AdmWatermarkUpdateDTO();
        dto.setWatermarkId(watermark.getString(COLUMN_WATERMARK_ID));
        dto.setWatermarkImg(watermark.getString(COLUMN_WATERMARK_IMG));
        dto.setWatermarkName(watermark.getString(COLUMN_WATERMARK_NAME));
        dto.setWatermarkPositiontype(watermark.getString(COLUMN_WATERMARK_POSITIONTYPE));
        dto.setCreatedAt(watermark.getString("created_at"));
        dto.setUpdatedAt(watermark.getString("updated_at"));
        return dto;
    }

    @Override
    public AdmWatermarkSetDefaultDTO defaultWatermark(AdmWatermarkSetDefaultVO vo) {
        AdmWatermarkSetDefaultDTO dto = new AdmWatermarkSetDefaultDTO();
        String watermarkId = vo.getWatermarkId();
        watermarkId = Optional.ofNullable(watermarkId).orElse("0");
        dto.setWatermarkId(watermarkId);
        roomInnerService.setDefaultWatermarkTemplateId(Integer.parseInt(watermarkId));
        return dto;
    }

    @Override
    public AdmWatermarkDeleteDTO deleteWatermark(AdmWatermarkDeleteVO vo) {
        Map<String, Object> param = new HashMap<>(8);
        param.put(COLUMN_WATERMARK_ID, vo.getWatermarkId());
        JSONObject jsonObject = Optional.ofNullable(paasService.deleteWatermark(param)).orElse(new JSONObject());
        AdmWatermarkDeleteDTO dto = new AdmWatermarkDeleteDTO();
        dto.setDeleteWatermarkId(jsonObject.getString("delete_watermark_id"));
        // 判断是否为默认水印 如果是默认水印则需要处理缓存和数据表
        String defaultWatermarkTemplateId = roomInnerService.getDefaultWatermarkTemplateId();
        if (defaultWatermarkTemplateId.equals(vo.getWatermarkId())) {
            redisTemplate.delete(RedisKey.DEFAULT_WATERMARK_TEMPLATE_KEY);
            roomInnerService.setDefaultWatermarkTemplateId(0);
        }
        roomInnerService.clearRoomWatermark(vo.getWatermarkId());
        return dto;
    }
}
